from tree_sitter import Language, Parser, Tree, Node
import tree_sitter_cpp
import random

class IdentifierGet:
    def __init__(self):
        self._declarator = ["array_declarator",
                           "attributed_declarator",
                           "destructor_name",
                           "function_declarator",
                           "identifier",
                           "operator_name",
                           "parenthesized_declarator",
                           "pointer_declarator",
                           "qualified_identifier",
                           "reference_declarator",
                           "structured_binding_declarator",
                           "template_function"]
        self._abstract_declarator = ["abstract_array_declarator",
                                     "abstract_function_declarator",
                                     "abstract_parenthesized_declarator",
                                     "abstract_pointer_declarator",
                                     "abstract_reference_declarator"]
        
    """
    return subtype of _declarator and _abstract_declarator
    """
    def get_subtype_of_declarator(self) -> list:
        return self._declarator
    
    def get_subtype_of_abstract_declarator(self) -> list:
        return self._abstract_declarator

    """
    _declarator
    """
    def array_declarator(node: Node):
        def traverse(node: Node):
            for child in node.named_children:
                if child.type == "array_declarator":
                    return traverse(child)
                elif child.type == "identifier":
                    return child.text.decode("utf-8")

        array_name = traverse(node)
        return {"type": "array", "identifier": array_name}
    
    def attributed_declarator(node: Node):
        print("identifier get failed. type: attributed_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def destructor_name(node: Node):
        print("identifier get failed. type: destructor_name, text: " + node.text.decode("utf-8"))
        return False
    
    def function_declarator(node: Node):
        print("identifier get failed. type: function_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def identifier(node: Node):
        return {"type": "identifier", "identifier": node.text.decode("utf-8")}
    
    def operator_name(node: Node):
        print("identifier get failed. type: operator_name, text: " + node.text.decode("utf-8"))
        return False
    
    def parenthesized_declarator(node: Node):
        print("identifier get failed. type: parenthesized_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def pointer_declarator(node: Node):
        def traverse(node: Node):
            for child in node.children:
                if child.type == "pointer_declarator":
                    return traverse(child)
                elif child.type == "identifier":
                    return child.text.decode("utf-8")

        pointer_name = traverse(node)
        return {"type": "pointer", "identifier": pointer_name}
    
    def qualified_identifier(node: Node):
        print("identifier get failed. type: qualified_identifier, text: " + node.text.decode("utf-8"))
        return False
    
    def reference_declarator(node: Node):
        print("identifier get failed. type: reference_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def structured_binding_declarator(node: Node):
        print("identifier get failed. type: structured_binding_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def template_function(node: Node):
        print("identifier get failed. type: template_function, text: " + node.text.decode("utf-8"))
        return False
    
    """
    abstract_declarator
    """
    def abstract_array_declarator(node: Node):
        print("identifier get failed. type: abstract_array_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def abstract_function_declarator(node: Node):
        print("identifier get failed. type: abstract_function_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def abstract_parenthesized_declarator(node: Node):
        print("identifier get failed. type: abstract_parenthesized_declarator, text: " + node.text.decode("utf-8"))
        return False
    
    def abstract_pointer_declarator(node: Node):
        return {"type": "abstract_pointer", "identifier": ""}
    
    def abstract_reference_declarator(node: Node):
        print("identifier get failed. type: abstract_reference_declarator, text: " + node.text.decode("utf-8"))
        return False